import { Component, Input, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { environment } from '@env/environment';
import { NzModalService } from 'ng-zorro-antd/modal';
import { NzNotificationService } from 'ng-zorro-antd/notification';
import { SystemChildrenDictEditComponent } from '../children-edit/children-edit.component';
import { SystemDictListEditComponent } from '../list-edit/list-edit.component';
import { SystemDictService } from '../service/service.service';

@Component({
  selector: 'app-list-children',
  templateUrl: './list-children.component.html',
  styles: [],
})
export class SystemDictChildrenComponent implements OnInit {
  @Input()
  record: any;

  searchForm: any = FormGroup;
  yesOrNo = environment.yesNo;
  isRun: number = -1;
  loading1 = false;
  dataList: any;
  pageInfo = {
    pi: 1,
    ps: 10,
    total: 0,
    loading: false,
  };
  switchValue = false;

  constructor(
    private fb: FormBuilder,
    private systemDictService: SystemDictService,
    private modalService: NzModalService,
    private notification: NzNotificationService,
  ) {}

  ngOnInit(): void {
    this.searchForm = this.fb.group({
      name: null,
      dictKey: null,
      dictValue: null,
      isRun: null,
    });
    this.load();
  }

  add(): void {
    this.modalService
      .create({
        nzContent: SystemChildrenDictEditComponent,
        nzComponentParams: { code: this.record.code, parentId: this.record.id },
        nzTitle: '检查清单库',
        nzFooter: null,
      })
      .afterClose.subscribe((res: any) => {
        if (res) {
          this.load();
        }
      });
  }

  edit(record: any): void {
    this.modalService
      .create({
        nzContent: SystemChildrenDictEditComponent,
        nzComponentParams: { record, code: this.record.code, parentId: this.record.id },
        nzTitle: '检查清单库',
        nzFooter: null,
      })
      .afterClose.subscribe((res: any) => {
        if (res) {
          this.load();
        }
      });
  }

  reset(): void {
    this.pageInfo.pi = 1;
    this.searchForm.reset();
    this.load();
  }

  search(): void {
    this.pageInfo.pi = 1;
    this.load();
  }
  //
  // remove(record: any) {
  //   this.gridAreaService.gridAreaManagementGroupDelete(record).subscribe(() => {
  //     this.load();
  //   });
  // }

  load(): void {
    const params = {
      ...this.searchForm.value,
      current: this.pageInfo.pi,
      size: this.pageInfo.ps,
      parentId: this.record.id,
    };
    //删除没有的属性和空的
    for (let key in params) {
      if (params.hasOwnProperty(key) && (params[key] == null || params[key] == undefined || params[key] === ' ')) {
        delete params[key];
      }
    }
    this.pageInfo.loading = true;
    this.systemDictService.childrenPage(params).subscribe((res) => {
      this.dataList = res.data.records;
      this.pageInfo.total = res.data.total;
      this.pageInfo.loading = false;
    });
  }

  remove(id: string): void {
    this.systemDictService.remove(id).subscribe((res) => {
      this.load();
    });
  }

  clickSwitch(data: any): void {
    const change = {
      id: data['id'],
      isRun: -1,
    };
    if (data['isRun'] == 1) {
      change['isRun'] = 0;
    } else {
      change['isRun'] = 1;
    }
    // this.pageInfo.loading = true;
    this.systemDictService.changeIsRun(change).subscribe((res) => {
      this.load();
      // this.pageInfo.loading = false;
    });
  }
}
